package com.zm.rpc.server.dispatcher;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.zm.rpc.protocol.Header;
import com.zm.rpc.protocol.RpcProtocol;
import com.zm.rpc.protocol.enums.RequestType;
import com.zm.rpc.server.dispatcher.handler.IDispatcherHandler;


@Component
public class Dispatcher implements IDispatcher{
    private static final Logger log = Logger.getLogger(Dispatcher.class);
    @Override
    public Object execute(RpcProtocol<Object> req) {
        log.debug(String.format("Request received:%s" ,req.toString()) );
        Object result = req.getContext();
        Header header = req.getHeader();
        IDispatcherHandler handler = IDispatcherHandler.getHandler(RequestType.valueOf(header.getReqType()));
        result = handler.doExecute(req.getContext());
        return result;
    }
    
}
